﻿using AutoTest.Common;
using AutoTest.Common.UtilsHelper;

namespace AutoTest.Domain
{

    /// <summary>
    /// 所有实体得基类，空实现，表示这是一个实体
    /// </summary>
    public class AllBaseEntity
    {

    }

    /// <summary>
    /// 基类
    /// </summary>
    /// <typeparam name="TPrimaryKey"></typeparam>
    public class BaseEntity<TPrimaryKey> : AllBaseEntity where TPrimaryKey : struct
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public BaseEntity()
        {

        }
        /// <summary>
        /// 主键
        /// </summary>
        public TPrimaryKey Id { get; set; }

        /// <summary>
        /// 重载==符号
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public static bool operator ==(BaseEntity<TPrimaryKey> left, BaseEntity<TPrimaryKey> right)
        {
            if (left.IsNull() || right.IsNull()) return false;
            return left.Id.Equals(right.Id); //left.Id == right.Id;
        }

        /// <summary>
        /// 重载!=符号
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public static bool operator !=(BaseEntity<TPrimaryKey> left, BaseEntity<TPrimaryKey> right)
        {
            return !(left == right);
        }

        /// <summary>
        /// 重载equal方法
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object? obj)
        {
            if (obj == null) return false;
            if (obj.Is<BaseEntity<TPrimaryKey>>())
            {
                return this == obj.CastTo<BaseEntity<TPrimaryKey>>();
            }
            else return false;
        }

        /// <summary>
        /// 重载方法
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }
        /// <summary>
        /// /// <summary>
        /// 重载方法
        /// </summary>
        /// <returns></returns>
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return base.ToString()??"";
        }
    }

    /// <summary>
    /// 默认的基类（主键为long类型)
    /// </summary>
    public class BaseEntity : BaseEntity<long>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public BaseEntity()
        {
            Id = IdHelper.NextId();
        }
    }

}
